<?php
/**
 * Smart Library - Smart Framework
 * http://smart-framework.highet.eu
 *
 * @category   SmartL
 * @package    SmartL_Application
 * @subpackage Config
 * @copyright  Copyright (c) 2005-2008 Smart Framework team - Stepan Sindelar, Vojtech Vit
 * @license    http://www.opensource.org/licenses/bsd-license.php	New BSD License
 */

/**
 * Holds data of one language and culture.
 * It has two separated informations: language (and also its name) and culture (and also its name). 
 * 
 * @todo 23: Předělat podle vzoru "hodnotový typ - neměnná"
 * 
 * @package SmartL_Application
 * @subpackage Config
 */
class SmartL_Application_Config_LanguageInfo {
	/**
	 * Two letters language identificator.
	 *
	 * @var string
	 */
	private $_languageId;
	/**
	 * Two letters culture identificator.
	 *
	 * @var string
	 */
	private $_cultureId;
	/**
	 * Name of language in this language.
	 *
	 * @var string
	 */
	private $_languageName;
	/**
	 * Name of culture in this language and its culture.
	 *
	 * @var string
	 */
	private $_cultureName;
	
	/**
	 * Cunstructor with support for creating language from ISO id.
	 *
	 * @param string $id ISO id of language.
	 */
	public function __construct($id = null) {
		if ($id !== null) {
			$this->setId ( $id );
		}
	}
	
	/**
	 * Returns language id in 2 lowercase letters format.
	 *
	 * @return string 2 lowercase letters long language id
	 */
	public function getLanguageId() {
		return $this->_languageId;
	}
	/**
	 * Sets language id in 2 lowercase letters format.
	 *
	 * @param string $value New 2 lowercase letters long language id
	 */
	public function setLanguageId($value) {
		$this->_languageId = strtolower ( $value );
	}
	/**
	 * Returns culture id in 2 uppercase letters format.
	 *
	 * @return string 2 uppercase letters long culture id
	 */
	public function getCultureId() {
		return $this->_cultureId;
	}
	/**
	 * Sets culture id in 2 uppercase letters format.
	 *
	 * @param string $value New 2 uppercase letters long culture id
	 */
	public function setCultureId($value) {
		if ($value !== strtoupper ( $value )) {
			require_once 'SmartL/Application/Config/LanguageInfo/Exception.php';
			throw new SmartL_Application_Config_LanguageInfo_Exception ( "Culture id is not in correct format" );
		}
		$this->_cultureId = $value;
	}
	/**
	 * Returns language name.
	 *
	 * @return string Language name
	 */
	public function getLanguageName() {
		return $this->_languageName;
	}
	/**
	 * Sets language name.
	 *
	 * @param string $value New language name
	 */
	public function setLanguageName($value) {
		$this->_languageName = $value;
	}
	/**
	 * Returns culture name
	 *
	 * @return string Culture name
	 */
	public function getCultureName() {
		return $this->_cultureName;
	}
	/**
	 * Returns full language name (in "%lang. name% - %culture name%" format) 
	 *
	 * @return string Language name
	 */
	public function getName() {
		return $this->getLanguageName () . " - " . $this->getCultureName ();
	}
	/**
	 * Sets culture name
	 *
	 * @param string $value New culture name
	 */
	public function setCultureName($value) {
		$this->_cultureName = $value;
	}
	/**
	 * Returns full language id in ISO "language-CULTURE" format.
	 *
	 * @return string Full language id
	 */
	public function getId() {
		return $this->_languageId . '-' . $this->_cultureId;
	}
	/**
	 * From given id creates language info-culture and language ids.
	 *
	 * @param string $value LanguageId-CultureId
	 */
	public function setId($value) {
		$parts = explode ( "-", $value );
		if (count ( $parts ) != 2 or $parts [1] !== strtoupper ( $parts [1] )) {
			require_once 'SmartL/Application/Config/LanguageInfo/Exception.php';
			throw new SmartL_Application_Config_LanguageInfo_Exception ( "Language id is not in correct format" );
		}
		$this->_languageId = $parts [0];
		$this->_cultureId = $parts [1];
	}
	/**
	 * Returns full language id in Zend "language_CULTURE" format.
	 *
	 * @return string Full language id
	 */
	public function getZendId() {
		return $this->_languageId . '_' . $this->_cultureId;
	}
	/**
	 * From given id creates language info-culture and language ids.
	 *
	 * @param string $value LanguageId_CultureId
	 */
	public function setZendId($value) {
		$parts = explode ( "_", $value );
		if (count ( $parts ) != 2) {
			require_once 'SmartL/Application/Config/LanguageInfo/Exception.php';
			throw new SmartL_Application_Config_LanguageInfo_Exception ( "Wrong language id given" );
		}
		$this->_languageId = $parts [0];
		$this->_cultureId = $parts [1];
	}
	/**
	 * Compares language id with supplied argument using both "-" and "_"
	 * culture separators (ISO and Zend format). Returns true if ids are equal.
	 *
	 * @param string|SmartL_Application_Config_LanguageInfo $id Compared language id
	 * @return bool True if language ids are equal
	 */
	public function equals($id) {
		if ($id instanceof SmartL_Application_Config_LanguageInfo) {
			return ($id->getId () == $this->getId ());
		} else {
			return ($id == $this->getId () or $id == $this->getZendId ());
		}
	}
}